
require "framework/BaseController"
require "framework/BaseView"
require "Exm/BallView"
require "Exm/MyModel"

local TickManager = CS.PengLuaKit.TickManager;

BallMediator = BaseController:New({mControllerName="BallMediator"});

function BallMediator:Awake()
	-- body
	-- Just load view skin async!
	self.mRootPanel = GameObject.Find("Canvas/Panel");
	self:OnViewCreated(self.mRootPanel);
end

function BallMediator:InitView(go)
	-- view start parse
	BallView:Awake(self.mRootPanel);

	self:InstallEventListener(true);
end

function BallMediator:InstallEventListener(isInstall)
	-- body
	local tickManager = TickManager.Instance();
	if isInstall then
		tickManager:AddFrameTickListener(self.OnUpdateTickHandler, false);
		self:AddEventListener("CircleCountChangeEvent", self.OnModelChangeListener);
	else
		tickManager:RemoveFrameTickListener(self.OnUpdateTickHandler);
		self:RemoveListener("CircleCountChangeEvent", self.OnModelChangeListener);
	end	
end

function BallMediator:OnUpdateTickHandler()
	-- body
	if Input.GetMouseButtonUp(0) == true then
		MyModel:AddCircle();
		BallView:AddBall();
	end
end

function BallMediator:OnModelChangeListener()
	-- body
	BallView:RefreshTextView(MyModel:GetCount());
end

function BallMediator:Dispose()
	-- body
	self:InstallEventListener(false);
	BallView:Dispose();
end